package com.sharainy.util;



import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {
    /**
     * @Author:whf
     * @param:
     * @Description: 获得“今天”零点时间戳
     */
    public static Long getTodayZeroPointTimestamps(){
        Long currentTimestamps= System.currentTimeMillis()/1000;
        Long oneDayTimestamps= Long.valueOf(60*60*24*1000)/1000;
        return currentTimestamps-(currentTimestamps+60*60*8*1000)%oneDayTimestamps;
    }

    /**
     * 获取当月第一天时间戳
     * @param time
     * @return
     */
    public static long getStartTime(String time){
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = sdf.parse(time+"-01");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime()/1000;
    }
    public static long getEndTime(String time){
        String[] timeStr =time.split("-");
        int year = Integer.parseInt(timeStr[0]);
        int month = Integer.parseInt(timeStr[1]);
        Calendar c = Calendar.getInstance();
        c.set(year, month, 0); //输入类型为int类型

        int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);

        return getStartTime(time)+3600*24*dayOfMonth;
    }

    /**
     * 获取平均值
     * @param list
     * @return
     */
    public static double getAverage(List<Double> list) {
        double sum = 0;
        for(int i=0;i<list.size();i++){
            sum += list.get(i).doubleValue();
        }
        return sum / list.size();
    }

    /**
     * 求和
     * @param list
     * @return
     */
    public static double getSum(List<Double> list) {
        double sum = 0;
        for(int i=0;i<list.size();i++){
            sum += list.get(i).doubleValue();
        }
        return sum ;
    }
    public static String getcode(String companyId){
    //得到long类型当前时间
    long l = System.currentTimeMillis();
    //new日期对
    Date date = new Date(l);
    //转换提日期输出格式
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
     return dateFormat.format(date)+companyId;
    }

    /**
     * 获取某月第一天时间戳
     * @param year
     * @param month
     * @return
     * @throws ParseException
     */
    public static String getFirstDayOfMonth(int year, int month)  {

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month-1);
        cal.set(Calendar.DAY_OF_MONTH,cal.getMinimum(Calendar.DATE));
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(new SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return   date.getTime()/1000+"";
    }

    /**
     * 获取当前年份
     * @return
     */
    public static String getSysYear() {
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));
        return year;
    }
    /**
     * 获取当前年份
     * @return
     */
    public static String getSysYearMonth() {
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));
        String month = String.valueOf(date.get(Calendar.MONTH)+1);
        if (month.length()<2){
            month="0"+month;
        }
        return year+"-"+month;
    }

    /**
     * 两个浮点值相加
     * @param d1
     * @param d2
     * @return
     */
    public static float add(float d1,float d2){
        BigDecimal b1 = new BigDecimal(Float.toString(d1)+"");
        BigDecimal b2 = new BigDecimal(Float.toString(d2)+"");
        return b2.add(b1).floatValue();
    }
    /* //时间戳转换日期 */
    public static String stampToTime(String stamp) {
    String sd = "";
    Date d = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    sd = sdf.format(new Date(Long.parseLong(stamp))); // 时间戳转换日期
    return sd;
    }

    /**
     * 时间戳转换成日期格式字符串
     * @param seconds 精确到秒的字符串
     * @param format
     * @return
     */
    public static String timeStamp2Date(String seconds, String format) {
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));
    }
    /**
     * 日期格式字符串转换成时间戳
     * @param date_str 字符串日期
     * @param format 如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String date2TimeStamp(String date_str, String format){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(date_str).getTime()/1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 取得当前时间戳（精确到秒）
     * @return
     */
    public static String timeStamp(){
        long time = System.currentTimeMillis();
        String t = String.valueOf(time/1000);
        return t;
    }

   /* public static void main(String[] args) {
        String timeStamp = timeStamp();
        System.out.println("timeStamp="+timeStamp); //运行输出:timeStamp=1470278082
        System.out.println(System.currentTimeMillis());//运行输出:1470278082980
        //该方法的作用是返回当前的计算机时间，时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数

        String date = timeStamp2Date(timeStamp, "yyyy-MM-dd HH:mm:ss");
        System.out.println("date="+date);//运行输出:date=2016-08-04 10:34:42

        String timeStamp2 = date2TimeStamp(date, "yyyy-MM-dd HH:mm:ss");
        System.out.println(timeStamp2);  //运行输出:1470278082
    }*/

    /**
     * 将中文数字转换为 阿拉伯数字
     * @param chineseNumber 中文数字字符串
     * @return 转换后的阿拉伯数字
     */
    public static long chineseNumber2Int(String chineseNumber) {
        String aval = "零一二三四五六七八九";
        String bval = "十百千万亿";
        int[] bnum = {10, 100, 1000, 10000, 100000000};
        long num = 0;
        char[] arr = chineseNumber.toCharArray();
        int len = arr.length;
        Stack<Integer> stack = new Stack<Integer>();
        for (int i = 0; i < len; i++) {
            char s = arr[i];
            //跳过零
            if (s == '零') continue;
            //用下标找到对应数字
            int index = bval.indexOf(s);
            //如果不在bval中，即当前字符为数字，直接入栈
            if (index == -1) {
                stack.push(aval.indexOf(s));
            } else { //当前字符为单位。
                int tempsum = 0;
                int val = bnum[index];
                //如果栈为空则直接入栈
                if (stack.isEmpty()) {
                    stack.push(val);
                    continue;
                }
                //如果栈中有比val小的元素则出栈，累加，乘N，再入栈
                while (!stack.isEmpty() && stack.peek() < val) {
                    tempsum += stack.pop();
                }
                //判断是否经过乘法处理
                if (tempsum == 0) {
                    stack.push(val);
                } else {
                    stack.push(tempsum * val);
                }
            }
        }
        //计算最终的和
        while (!stack.isEmpty()) {
            num += stack.pop();
        }
        return num;
    }
    /**
     * 截取第一处的中文数字字符串
     * @param str
     * @return
     */
    public static String getNumberStr(String str) {
        StringBuilder stringBuilder = new StringBuilder();
        boolean isFirst = true;
        String chineseNumStr="零一二三四五六七八九十百千万亿";
        for (int i = 0; i < str.length(); i++) {
            String tempStr = str.substring(i, i + 1);
            if (chineseNumStr.contains(tempStr)){
                stringBuilder.append(tempStr);
                if (isFirst) {
                    isFirst = false;
                }
            } else {
                if (!isFirst) {
                    break;
                }
            }
        }
        return stringBuilder.toString();
    }
    /**
     * 获取两个ArrayList的差集
     * @param firstArrayList 第一个ArrayList
     * @param secondArrayList 第二个ArrayList
     * @return resultList 差集ArrayList
     */
    public static List<String> receiveDefectList(List<String> firstArrayList, List<String> secondArrayList) {
        List<String> resultList = new ArrayList<String>();
        LinkedList<String> result = new LinkedList<String>(firstArrayList);// 大集合用linkedlist
        HashSet<String> othHash = new HashSet<String>(secondArrayList);// 小集合用hashset
        Iterator<String> iter = result.iterator();// 采用Iterator迭代器进行数据的操作
        while(iter.hasNext()){
            if(othHash.contains(iter.next())){
                iter.remove();
            }
        }
        resultList = new ArrayList<String>(result);
        return resultList;
    }
}
